using AntdUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FolderBrowserDialog = System.Windows.Forms.FolderBrowserDialog;

namespace TangLiveRecorder.Controls
{
    public partial class SettingControl : UserControl
    {
        private Form _form;

        public SettingControl(MainWindow form)
        {
            InitializeComponent();
            this.Tag = "Setting";
            this.Dock = DockStyle.Fill;
            _form = form;
            InitializeSettings();
            LoadData();
        }

        private void LoadData()
        {
            inputPollingInterval.Value = Data.settingInfo.PollingInterval;
            inputSavePath.Text = Data.settingInfo.savePath;
            switchAutoStart.Checked = Data.settingInfo.AutoStart;
            inputMaxFileSize.Value = Data.settingInfo.maxFileSize;
        }

        private void InitializeSettings()
        {
            // 初始化视频质量选项
            // 绑定事件
            btnSave.Click += BtnSave_Click;
            btnBrowse.Click += BtnBrowse_Click;
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                // 保存设置逻辑
                var pollingInterval = (int)inputPollingInterval.Value;
                var savePath = inputSavePath.Text;
                var autoStart = switchAutoStart.Checked;
                var maxFileSize = (int)inputMaxFileSize.Value;

                Data.settingInfo.PollingInterval = pollingInterval;
                Data.settingInfo.savePath = savePath;
                Data.settingInfo.AutoStart = autoStart;
                Data.settingInfo.maxFileSize = maxFileSize;

                Data.SaveSetting();

                AntdUI.Message.Config config = new AntdUI.Message.Config(this._form, $"设置保存成功", TType.Success);
                config.ShowInWindow = true;
                config.open();
            }
            catch (Exception ex)
            {
                AntdUI.Message.Config config = new AntdUI.Message.Config(this._form, $"保存设置失败：{ex.Message}", TType.Warn);
                config.ShowInWindow = true;
                config.open();
            }
        }

        private void BtnBrowse_Click(object sender, EventArgs e)
        {
            using (var folderDialog = new FolderBrowserDialog())
            {
                folderDialog.Description = "选择录制文件保存路径";
                folderDialog.ShowNewFolderButton = true;

                if (folderDialog.ShowDialog() == DialogResult.OK)
                {
                    inputSavePath.Text = folderDialog.SelectedPath;
                }
            }
        }
    }
}